<template>
  <view>
    <view class="goodsList">
      <my-goods v-for="(item,index) in goodsList" :key="item.goods_id" :item="item" @click="gotoDetail"></my-goods>
    </view>
  </view>
</template>

<script>
  
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[] ,
        isLoading:false,
        total:0,
      };
    },
    onLoad(option) {
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(func){
        this.isLoading = true
        try{
          const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
          if(res.meta.status!==200){
            return uni.$showMsg()
          }
          this.goodsList = [...this.goodsList,...res.message.goods]
          this.total = res.message.total
        }
        finally{
          this.isLoading = false
          func&&func()
        }  
      },
      gotoDetail(id){
        uni.navigateTo({
          url:`/subpackage/goods_detail/goods_detail?goods_id=${id}`
        })
      }
    },
    onReachBottom() {
      if(this.isLoading){
        return
      }
      if(this.queryObj.pagenum*this.queryObj.pagesize>=this.total){
        return uni.$showMsg('数据加载完毕')
      }
      this.queryObj.pagenum+=1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum=1
      this.goodsList=[]
      this.total = 0
      this.getGoodsList(()=>uni.stopPullDownRefresh())   
    }
  }
</script>

<style lang="scss">

</style>
